<template>
  <div>
    <el-form :model="tblSample" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="活动名称" prop="activityName">
        <el-input
          v-model="tblSample.activityName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样人" prop="name">
        <el-input
          v-model="tblSample.name"
          placeholder="请输入采样人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测结果" prop="sampleResult">
        <el-select v-model="tblSample.sampleResult" placeholder="检测结果" clearable>
          <el-option
            v-for="(item,index) in sampleResult"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测状态" prop="status">
        <el-select v-model="tblSample.status" placeholder="检测状态" clearable>
          <el-option
            v-for="(item,index) in status"
            :key="index"
            :label="item"
            :value="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采样时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center">
      </el-table-column>
      <el-table-column
        prop="tblUser.name"
        label="姓名"
        width="120"
        align="center">
      </el-table-column>
      <el-table-column
        prop="tblActivities.activityName"
        label="活动名称"
        width="120"
        align="center">
      </el-table-column>
      <el-table-column
        prop="tblUnits.name"
        label="单位名称"
        width="120"
        align="center">
      </el-table-column>
      <el-table-column
        label="采样时间"
        width="160"
        align="center">
        <template slot-scope="scope">{{ scope.row.sampleTime }}</template>
      </el-table-column>
      <el-table-column
        label="检测时间"
        width="160"
        align="center">
        <template slot-scope="scope">{{ scope.row.testTime }}</template>
      </el-table-column>
      <el-table-column
        prop="sampleResult"
        label="检测结果"
        width="80"
        align="center">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="120"
        align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.status===1"><el-tag type="danger">未检测</el-tag></div>
          <div v-else><el-tag type="success">已检测</el-tag></div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10,15,30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getAllSampleByPage,deleteSampleRecord,exportSampleRecord } from "../../api/testManagement/record";

export default {
  name: "index",
  data() {
    return {
      tblSample:{
        pageNum:1,
        pageSize:10,
        unitsId: 1,
        name:'',
        activityName:'',
        sampleResult:'',
        unitsName:'',
        status:''
      },
      dateRange:[],
      tableData: [
        {
          sampleRecordId:1,
          tblUser:{
            name:'张三'
          },
          tblActivities:{
            activityName:"核酸检测"
          },
          tblUnits:{
            name:'赣南医学院'
          },
          sampleTime:"2022-07-24 09:00:00",
          testTime:"2022- 07-24 09:30:00",
          sampleResult: "阴性",
          status:1
        }],
      ids: [],
      currentPage:1,
      pageSize:10,
      total:10,
      multiple: true,
      status:['已检测','已扫码'],
      sampleResult: ['阴性','阳性']
    }
  },
  created() {
    this.getAllSampleByPage()
  },
  methods: {
    getAllSampleByPage() {
      getAllSampleByPage(this.tblSample).then(res => {
        // console.log(res.data)
        this.tblSample.currentPage = res.data.current
        this.tblSample.pageSize = res.data.pageSize
        this.total = res.data.total
        this.tableData = res.data.list
        if (this.tblSample.beginTime !== '' || this.tblSample.endTime !== '') {
          this.tblSample.beginTime = ''
          this.tblSample.endTime = ''
        }
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activitiesId)
      this.multiple = !selection.length
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.tblSample.pageSize = val
      this.getAllSampleByPage()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.tblSample.pageNum = val
      this.getAllSampleByPage()
    },
    handleQuery() {
      this.tblSample.currentPage = 1
      if (this.dateRange) {
        this.tblSample.beginTime = this.dateRange[0]
        this.tblSample.endTime = this.dateRange[1]
      }
      this.getAllSampleByPage()
    },
    resetQuery(formName) {
      this.tblSample.beginTime = ''
      this.tblSample.endTime = ''
      this.dateRange = []
      this.$refs[formName].resetFields();
    },
    handleDelete(row) {
      const sampleRecordIds = row.sampleRecordId || this.ids;
      this.$confirm('是否确认删除记录编号为"'+sampleRecordIds+'"的数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return deleteSampleRecord(sampleRecordIds);
      }).then(() => {
        this.getAllSampleByPage()
        this.$message({
          message:'删除成功',
          type:'success'
        })
      })
    },
    handleExport() {
      exportSampleRecord(this.tblSample).then(res => {
        let blob = new Blob([res.data],{type:'application/vnd.ms-excel'})
        let filename = window.decodeURI(res.headers['content-disposition']).split('=')[1]
        let url = window.URL.createObjectURL(blob)
        let aLink = document.createElement("a")
        aLink.style.display = "none"
        aLink.href = url
        aLink.setAttribute("download",filename)
        document.body.appendChild(aLink)
        aLink.click()
        document.body.removeChild(aLink)
        window.URL.revokeObjectURL(url)
      })
    }
  },
  components: {

  }
}
</script>

<style scoped>

</style>
